/*******************************************************************************
 * Copyright (c) 2010 liw.
 * All rights reserved. 
 * 
 * This file is part of VanBus.
 * 
 *     VanBus is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     VanBus is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 * 
 *     You should have received a copy of the GNU General Public License
 *     along with VanBus.  If not, see <http://www.gnu.org/licenses/>.
 * Contributors:
 *     liw - initial API and implementation
 ******************************************************************************/
package org.niclab.vanbus.query.ws;

import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.http.HttpEntity;
import org.apache.http.util.EntityUtils;
import org.niclab.vanbus.model.BusStop;
import org.niclab.vanbus.query.IQuery;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.util.Log;

public class StopCoordsWSQueryResResolver extends BaseResponseResolver<StopCoordsWSQueryResResolver.QueryResult>{


	//private BusStop stop;
	private static final String LOG_TAG="StopCoordinatesWSQueryResHandler";
	
	public static class QueryResult {
		private double lat;
		private double lng;
		public double getLat() {
			return lat;
		}
		public void setLat(double lat) {
			this.lat = lat;
		}
		public double getLng() {
			return lng;
		}
		public void setLng(double lng) {
			this.lng = lng;
		}
		
			
	}

	

	@Override
	protected QueryResult interpret(HttpEntity entity) {
		
		QueryResult result = new QueryResult();
		InputStream inputStream = null;
		try {
			inputStream = entity.getContent();
			/*SAXParserFactory saxFactory=SAXParserFactory.newInstance();
			SAXParser parser = saxFactory.newSAXParser();
			parser.getXMLReader();*/
			XmlPullParser xmlParser =XmlPullParserFactory.newInstance().newPullParser();
			xmlParser.setInput(inputStream, null);
			int eType= xmlParser.getEventType();
			while(eType!= XmlPullParser.END_DOCUMENT){
				switch(eType){
				case XmlPullParser.START_TAG:
					Log.v(LOG_TAG,"tag name:" + xmlParser.getName());
					if(xmlParser.getName().equals("coordinates"))
					{
						String[] coor = xmlParser.nextText().split(",");
						Log.d(LOG_TAG,"got stop's coordinates:" + coor[0]+" "+coor[1]);
						
						result.setLng(Double.parseDouble(coor[0]));
						result.setLat(Double.parseDouble(coor[1]));

					}
					break;
					
				}
				
				eType = xmlParser.next();
				
			}
		} catch (IllegalStateException e) {
			Log.d("LOG_TAG", "HttpEntity exception");
			e.printStackTrace();
		} catch (IOException e) {
			Log.d("LOG_TAG", "HttpEntity exception");
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			Log.d("LOG_TAG", "XmlPullParserException");
		
			e.printStackTrace();
		}
		
		
		
		return result;
	}
	
	
	

}
